package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.front.mall.converter.GoodsToCartConverter;
import cn.tedu.tmall.front.mall.dao.cache.ICartCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IGoodsRepository;
import cn.tedu.tmall.front.mall.pojo.po.CartPO;
import cn.tedu.tmall.front.mall.pojo.vo.CartListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private ICartCacheRepository cartCacheRepository;
    @Autowired
    private IGoodsRepository goodsRepository;

    @Override
    public void add(CurrentPrincipal currentPrincipal, Long goodsId, Integer goodsNum) {
        // 核心业务逻辑
        GoodsStandardVO goods = goodsRepository.getStandardById(goodsId);

        // 判断根据商品用户添加的商品id是否存在
        if (goods == null) {
            String message = "添加购物车失败,商品数据不存在!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        // 判断商品是否是上架商品
        if (goods.getIsPutOn() != 1) {
            String message = "添加购物车失败,商品已下架!";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        CartPO cartPO = new CartPO(goodsId,goodsNum);
        CartPO cartCache = cartCacheRepository.getByUserAndGoods(currentPrincipal.getId(), goodsId);
        // 判断商品是否添加过到购物车中
        if (cartCache != null) {
            cartPO.setGoodsNum(cartCache.getGoodsNum() + goodsNum);
        }
        cartCacheRepository.put(currentPrincipal.getId(),cartPO);
    }

    @Override
    public Integer increaseNum(CurrentPrincipal currentPrincipal, Long goodsId) {
        return increaseNum(currentPrincipal.getId(), goodsId,1);
    }

    @Override
    public Integer increaseNum(CurrentPrincipal currentPrincipal, Long goodsId, Integer num) {
        return increaseNum(currentPrincipal.getId(), goodsId, num);
    }

    @Override
    public Integer reduceNum(CurrentPrincipal currentPrincipal, Long goodsId) {
        return reduceNum(currentPrincipal.getId(), goodsId,1);
    }

    @Override
    public Integer reduceNum(CurrentPrincipal currentPrincipal, Long goodsId, Integer num) {
        return reduceNum(currentPrincipal.getId(), goodsId, num);
    }

    @Override
    public List<CartListItemVO> list(CurrentPrincipal currentPrincipal) {
        ArrayList<CartListItemVO> cartList = new ArrayList<>(); //创建购物车中要展现的对象
        List<CartPO> cartPOList = cartCacheRepository.listByUser(currentPrincipal.getId()); // 获取当事人的购物车列表信息
        for (CartPO cartPO : cartPOList) {
            GoodsStandardVO goods = goodsRepository.getStandardById(cartPO.getGoodsId()); //遍历根据商品id查询的商品详情
            if (goods != null) {
                CartListItemVO cartListItemVO = GoodsToCartConverter.convertToCart(cartPO, goods); //将商品数据转换为购物车显示对象的转换器
                cartList.add(cartListItemVO);
            }
        }
        return cartList;
    }

    /**
     * 增加购物车中的商品的数量
     *
     * @param userId  用户ID
     * @param goodsId 商品ID
     * @param num     增加的商品的数量
     * @return 新的数量
     */
    private Integer increaseNum(Long userId,Long goodsId,Integer num) {
        CartPO cartCache = cartCacheRepository.getByUserAndGoods(userId, goodsId);
        Integer newNum = cartCache.getGoodsNum() + num; // 新增后的数量

        // 你可能需要在此检查新的数量是否超出允许的范围

        CartPO cartPO = new CartPO(goodsId, newNum);
        cartCacheRepository.put(userId , cartPO);
        return newNum;
    }

    /**
     * 减少购物车中的商品的数量
     *
     * @param userId  用户ID
     * @param goodsId 商品ID
     * @param num     减少的商品的数量
     * @return 新的数量
     */
    private Integer reduceNum(Long userId,Long goodsId,Integer num) {
        // 核心业务逻辑

        // 根据缓存数据查询是否含有对应的用户ID以及商品ID
        CartPO cartCache = cartCacheRepository.getByUserAndGoods(userId, goodsId);

        // 判断购物车商品是否存在
        if (cartCache != null) {
            return 0;
        }

        // 用户减少购物车商品数量本能小于等于0
        Integer newNum = cartCache.getGoodsNum() - num;
        if (newNum <= 0) {
            cartCacheRepository.deleteByUserAndGoods(userId, goodsId);
            return 0;
        }

        CartPO cartPO = new CartPO(goodsId, newNum);
        cartCacheRepository.put(userId,cartPO);
        return newNum;
    }

}
